// components/cartPopup.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: {}
    },
  },

  // 组件加载完成
  attached: function () {
    this.setData({
      cartData: this.properties.data
    })
    console.log(this.properties.data)
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {

    // 购物车弹窗 显示隐藏
    closePopup() {
      if (this.properties.cartData.total_num < 1){
        return
      }
      this.setData({
        showCart: !this.data.showCart
      })
    },

    // 清空购物车
    clearCart() {
      this.setData({
        showCart: false
      })
    },

    // 阻止事件冒泡
    nothing() {
    },

    // 购物车数量变化
    // 减
    cartCut(e) {
      let item = e.currentTarget.dataset.item, that = this, index = e.currentTarget.dataset.index;
      console.log(item);
      if(item.num == 1){
        wx.request({
          url: app.baseURL + 'item/delShopingCart',
          data: {
            token: app.globalData.userId,
            ids: item.id
          },
          success(data) {
            let ress = data.data;
            if (ress.code == 1) {
              if (that.data.cartData.list.length == 1){
                that.setData({
                  showCart:false
                })
              }
              that.properties.cartData.list.splce(index,1)
            } else {
              app.$tip(ress.msg)
            }
          }
        })
      }
      wx.request({
        url: app.baseURL + 'item/addShopingCartNums',
        data: {
          token: app.globalData.userId,
          id: id,
          num: 1,
          type: 2
        },
        success(data) {
          let res = data.data;
          wx.hideLoading();
          if (res.code == 1) {
            old[findex].num = Number(old[findex].num) - 1
            that.setData({
              list: old
            })
            that.getPrice();
          } else {
            app.$tip(res.msg)
          }

        }
      })
    },

    // 加
    cartAdd(e) {
      let item = e.currentTarget.dataset.item;
      console.log(item);
      return
      wx.request({
        url: app.baseURL + 'item/addShopingCartNums',
        data: {
          token: app.globalData.userId,
          id: id,
          num: 1,
          type: 1
        },
        success(data) {
          let res = data.data;
          wx.hideLoading();
          if (res.code == 1) {
            old[findex].num = Number(old[findex].num) + 1
            that.setData({
              list: old
            })
            that.getPrice();
          } else {
            app.$tip(res.msg)
          }
        }
      })
    },

    // 结算
    toBuy(){

    },
  }
})
